app.controller('mainCtrl', function ($scope, $http, $location, AuthorityService, URLService, localStorageService) {
    AuthorityService.check();

    // 国际化资源，用于访问公共i18n
    $scope.lang = SYSCONF.lang;
    $scope.I18N = LANG['LOCAL_' + $scope.lang.toUpperCase()];
    // 模块资源，用于访问当前模块i18n
    $scope.i18n = I18N.MODULE.SERVICE_CENTER;

    // 默认显示 配置服务参数
    $scope.checked = $location.path().slice(1);
    if (Ut.strUtils.isEmpty($scope.checked)) {
        $scope.checked = 'param';
    }

    $scope.role = AuthorityService.getRole();

    $scope.events = {
        //菜单点击事件
        menuItemClickEvt: function (type) {
            if ($scope.checked !== type) {
                $scope.checked = type;
            }
        },
        //修改密码确认事件
        modifyPsw: function () {
            $scope.errMsg = null;
            var url = URLService.getURL('modifyPassword');
            var info = angular.copy($scope.info);

            if (Ut.strUtils.isEmpty(info.oldPwd)) {
                $scope.errMsg = I18N.TIP.t7;
                return;
            }
            if (Ut.strUtils.isEmpty(info.newPwd)) {
                $scope.errMsg = I18N.TIP.t6;
                return;
            } else {
                var chrnum = /^(?![^a-zA-Z]+$)(?!\D+$).{8,16}$/;
                var value = info.newPwd;
                if (value.length < 8 || value.length > 16 || !chrnum.test(value)) {
                    $scope.errMsg = I18N.TIP.t10;
                    return;
                }
            }


            if (Ut.strUtils.isEmpty(info.ensurePwd)) {
                $scope.errMsg = I18N.TIP.t8;
                return;
            }

            if (info.ensurePwd != info.newPwd) {
                $scope.errMsg = I18N.TIP.t9;
                return;
            }

            if (info.newPwd === info.oldPwd) {
                $scope.errMsg = I18N.TIP.t16;
                return;
            }

            var param = {
                userName: $scope.role.userName,
                passWord: $scope.info.oldPwd,
                newPassWord: $scope.info.newPwd
            };

            $http.post(url, param).success(function (res) {
                if (res.success) {
                    $('#mdfpswd-div').modal('hide');
                    Ut.dialog.tip(I18N.TIP.t11);
                } else {
                    $scope.errMsg = res.errorMsg;
                }

            });
        },
        //注销
        quit: function () {
            //后台登出
            var promise = AuthorityService.quit();
            //如果成功则跳转到首页
            promise.then(function () {
                window.location = SYSCONF.index_url;
            }, function (reason) {
                Ut.dialog.errMsg(I18N.ERR_Msg.logoutFailure);
            });
        },
        //更改系统语言
        changeLanguage: function (lang) {
        //     if ($scope.lang !== lang) {
        //         $scope.lang = lang;
        //         localStorageService.set('language', lang);
        //         $scope.I18N = LANG['LOCAL_' + $scope.lang.toUpperCase()];
        //     }
        //
        }
    };

    //清除已输入信息
    $('#mdfpswd-div').on('hidden.bs.modal', function () {
        $scope.$apply(function () {
            $scope.info = {};
            $scope.errMsg = null;
        });

    })
});
